﻿using EventSys.MVC4Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebMatrix.WebData;

namespace EventSys.MVC4Web.Controllers
{
    public class RecuparContraseñaController : Controller
    {
        //
        // GET: /RecuparContraseña/

        public ActionResult Index()
        {
            return View();
        }
        private void SendEMail(string emailid, string subject, string body)
        {
            SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            client.Host = "smtp.gmail.com";
            client.Port = 587;

            //correo de origen
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("jorgegonzales868@gmail.com", "jorgegonzales2013");
            client.UseDefaultCredentials = false;
            client.Credentials = credentials;

            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("xxxxx@gmail.com");
            msg.To.Add(new MailAddress(emailid));

            msg.Subject = subject;
            msg.IsBodyHtml = true;
            msg.Body = body;

            client.Send(msg);
        }

        [HttpPost]
        public ActionResult Recu_Contraseña(string usuario)
        {
            //verificar usuario si existe en bd
            var user = Membership.GetUser(usuario);
            if (user == null)
            {
                TempData["Message"] = "El usuario no existe";
            }
            else
            {
                //generate password token
                var token = WebSecurity.GeneratePasswordResetToken(usuario);
                //create url with above token
                var resetLink = "<a href='" + Url.Action("ResetPassword", "Account", new { un = usuario, rt = token }, "http") + "'>Reset Password</a>";
                //get user emailid

                UsersContext db = new UsersContext();//accesar a las tablas del member
                var emailid =
                    (from i in db.UserProfiles
                     where i.UserName == usuario
                     select i.Correo)
                             .FirstOrDefault();
                //send mail
                string subject = "Password Reset Token";
                string body = "<b>Please find the Password Reset Token</b><br/>" + resetLink; //edit it
                try
                {
                    SendEMail(emailid, subject, body);
                    TempData["Message"] = "Email Enviado";
                }
                catch (Exception ex)
                {
                    TempData["Message"] = "Error occured while sending email." + ex.Message;
                }
                //only for testing
                TempData["Message"] = resetLink;
            }

            return View();

        }   


    }
}
